﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Dictionary
{
    public partial class ManufacturersDetail : Form
    {
        private Manufacturers _parent;
        public DataSets.Dictionary ManufacturerDate
        {
            get
            {
                return _parent.dictionary;
            }
        }
        private Int32 _manId;
        public Int32 ManufacturersId
        {
            get
            {
                return _manId;
            }
            set
            {
                _manId = value;
            }
        }
        private Util.ActionType _openAction;
        public Util.ActionType OpenAction
        {
            get
            {
                return this._openAction;
            }
            set
            {
                this._openAction = value;
            }
        }
        private DataSets.Dictionary.ManufacturersRow _row;
        public DataSets.Dictionary.ManufacturersRow CurrentRow
        {
            get
            {
                return this._row;
            }
            set
            {
                if (value == null)
                    throw new NullReferenceException("Не найден производитель в таблице производителей!");
                this._row = value;
            }
        }
        private ManufacturersDetail()
        {
            InitializeComponent();
        }
        public ManufacturersDetail(Util.ActionType actType, Int32 manufacturersId, Manufacturers parent)
        {
            InitializeComponent();
            OpenAction = actType;
            ManufacturersId = manufacturersId;
            _parent = parent;
            if (ManufacturersId != Int32.MinValue)
            {
                this.CurrentRow = this.ManufacturerDate.Manufacturers.FindById(ManufacturersId);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ManufacturersDetail_Load(object sender, EventArgs e)
        {
            InitCountry();
            switch (OpenAction)
            {
                case DIProject.Util.ActionType.Insert:
                    InitInsert();
                    break;
                case DIProject.Util.ActionType.Update:
                    InitUpdate();
                    break;
                default:
                    this.Close();
                    break;
            }
        }
        private void InitCountry()
        {            
            lbCountry.DataSource = ManufacturerDate.Countries;
            lbCountry.ValueMember = "Id";
            lbCountry.DisplayMember = "Name";                        
        }
        private void InitInsert()
        {
            btnDelete.Visible = false;
            btnSaveUpdate.Text = "Сохранить";
        }
        private void InitUpdate()
        {
            btnDelete.Visible = true;
            btnSaveUpdate.Text = "Изменить";
            if (ManufacturerDate.Countries.Count < 1)
            {
                throw new Util.DIException("Не заполнена таблица стран!");
            }
            DataSets.Dictionary.CountriesRow cr = ManufacturerDate.Countries.FindById(CurrentRow.CountryId);
            Int32 pos = lbCountry.FindString(cr.Name);
            lbCountry.SelectedIndex = pos;
            txbLatName.Text = CurrentRow.LatName;
            txbName.Text = CurrentRow.Name;
            txbNote.Text = CurrentRow.Note;
            txbShortName.Text = CurrentRow.ShortName;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Вы действительно хотите удалить производителя " + CurrentRow.Name + "?", "Удаление производителя", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                DataSets.DictionaryTableAdapters.ManufacturersTableAdapter adapter = new DIProject.DataSets.DictionaryTableAdapters.ManufacturersTableAdapter();
                if (DIProject.Data.Sync.IsAdmin)
                {
                    CurrentRow.MW = Properties.Resources.strPhysicallyDelete;
                }
                CurrentRow.Delete();
                adapter.Update(this.ManufacturerDate);
                ManufacturerDate.AcceptChanges();
            }
            this.Close();
        }

        private void btnSaveUpdate_Click(object sender, EventArgs e)
        {
            switch (OpenAction)
            {
                case DIProject.Util.ActionType.Update:
                    this.UpdateM();
                    break;
                case DIProject.Util.ActionType.Insert:
                    this.Insert();
                    break;
                default:
                    break;
            }
            this.Close();
        }
        private void Insert()
        {
            DataSets.DictionaryTableAdapters.ManufacturersTableAdapter adapter = new DIProject.DataSets.DictionaryTableAdapters.ManufacturersTableAdapter();
            DataSets.Dictionary.ManufacturersRow mr = ManufacturerDate.Manufacturers.NewManufacturersRow();
            mr.Name = txbName.Text;
            mr.ShortName = txbShortName.Text;
            mr.LatName = txbLatName.Text;
            mr.MD = DateTime.Now;
            mr.CountryId = (Int32)lbCountry.SelectedValue;
            mr.Note = txbNote.Text;
            mr.ToDelete = false;
            string MW = FirstForm.Login;
            if (string.IsNullOrEmpty(MW))
                MW = string.Empty;
            mr.MW = MW;
            ManufacturerDate.Manufacturers.Rows.Add(mr);
            adapter.Update(ManufacturerDate);
            mr.Original_MD = mr.MD;
            ManufacturerDate.AcceptChanges();
        }
        private void UpdateM()
        {
            DataSets.DictionaryTableAdapters.ManufacturersTableAdapter adapter = new DIProject.DataSets.DictionaryTableAdapters.ManufacturersTableAdapter();
            CurrentRow.Name = txbName.Text;
            CurrentRow.ShortName = txbShortName.Text;
            CurrentRow.LatName = txbLatName.Text;
            CurrentRow.MD = DateTime.Now;
            CurrentRow.CountryId = (Int32)lbCountry.SelectedValue;
            CurrentRow.Note = txbNote.Text;
            string MW = FirstForm.Login;
            if (string.IsNullOrEmpty(MW))
                MW = string.Empty;
            CurrentRow.MW = MW;
            CurrentRow.ToDelete = false;
            adapter.Update(CurrentRow);
            CurrentRow.Original_MD = CurrentRow.MD;
           
            ManufacturerDate.AcceptChanges();
        }

        private void txbName_Validating(object sender, CancelEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (string.IsNullOrEmpty(tb.Text))
            {
                MessageBox.Show("Значение поля \"Наименование\" не может быть пустым!", "Введите значение!");
                e.Cancel = true;
                tb.Focus();
            }
        }

        private void txbShortName_Validating(object sender, CancelEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (string.IsNullOrEmpty(tb.Text))
            {
                MessageBox.Show("Значение поля \" Короткое наименование\" не может быть пустым!", "Введите значение!");
                e.Cancel = true;
                tb.Focus();
            }
        }
    }
}